package cn.itsource.service.impl;

import cn.itsource.pojo.domain.Department;
import cn.itsource.mapper.DepartmentMapper;
import cn.itsource.pojo.domain.Employee;
import cn.itsource.pojo.dto.DepartmentDto;
import cn.itsource.pojo.query.PageQueryWrapper;
import cn.itsource.result.JSONResult;
import cn.itsource.service.IDepartmentService;
import cn.itsource.utils.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lifan
 * @since 2024-09-10
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public JSONResult selectDept(PageQueryWrapper<DepartmentDto> departmentDto) {
        List<DepartmentDto> departmentDtos;
        if (departmentDto.getKeyword() == null){
            departmentDtos = departmentMapper.seelctDept(departmentDto);
        }else {
            //条件查询
           departmentDtos = departmentMapper.seelctDept(departmentDto);
        }
        return JSONResult.success(departmentDtos);
    }

    /**
     * 添加部门
     * @param department
     * @return
     */
    @Override
    public JSONResult insert(Department department) {
        department.setId(IdUtil.getId());
        department.setSn(UUID.randomUUID().toString());
        departmentMapper.insert(department);
        return JSONResult.success();
    }

    /**
     * 修改部门
     * @param department
     * @return
     */
    @Override
    public JSONResult update(Department department) {
        Department department1 = departmentMapper.selectById(department.getId());
        if (department1 != null){
            department1.setName(department.getName());
            departmentMapper.updateById(department);
            return JSONResult.success();
        }
        return JSONResult.error();
    }

    @Override
    public List<DepartmentDto> typeTree() {
        // 创建一个集合用来装父级类型对象
        ArrayList<DepartmentDto> list = new ArrayList<>();
        // 查询所有数据
        List<DepartmentDto> all = departmentMapper.findAll();
        // hashMap在这起一个缓存作用，方便后续不用一直在数据库中查询子级对象
        HashMap<Long, DepartmentDto> hashMap = new HashMap<>();
        // 遍历查询的数据，以键值对的方式存储在map集合中
        for (DepartmentDto DepartmentDto : all) {
            // id作键，遍历的每个对象作值
            hashMap.put(DepartmentDto.getId(),DepartmentDto);
        }
        // 遍历数据库数据，判断每个对象有没有子级类型
        for (DepartmentDto DepartmentDto : all) {
            if (DepartmentDto.getParentId() == null){
                // 为空，就是没有，自己是父级类型，装进前面创建的list集合中
                list.add(DepartmentDto);
            }else {
                // 不为空，就拿到子级类型的id
                Long parentId = DepartmentDto.getParentId();
                // 根据子级类型id从缓存map集合中拿到这个子级对象
                DepartmentDto department = hashMap.get(parentId);
                if (department !=null){
                    // 获取到子级对象中的children集合
                    List<DepartmentDto> children = department.getChildren();
                    // 将自己装入children集合中
                    children.add(DepartmentDto);
                }
            }
        }
        return list;
    }

    @Override
    public List<Employee> getEmployeeName() {
        return departmentMapper.getEmployeeName();
    }

    @Override
    public void deleteId(Long[] ids) {
        departmentMapper.deleteId(ids);
    }
}
